<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('一级指标')" prop="indicators">
        <el-input
          v-model="queryParams.indicators"
          :placeholder="$t('一级指标')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bf:evaluation:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bf:evaluation:edit']"
          type="primary"
          icon="el-icon-check"
          size="mini"
          @click="submitSort"
        >{{ $t('保存排序') }}</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bfEvaluationDetailList"
      row-key="id"
      border
    >
      <el-table-column :label="$t('指标')" align="left" prop="indicators" min-width="280" />
      <el-table-column :label="$t('级别')" align="center" prop="grade" :formatter="gradeFormat" min-width="100" />
      <el-table-column :label="$t('权重')" align="center" prop="weight" min-width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.weight) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('排序')" align="center" prop="sort" min-width="100">
        <template slot-scope="scope">
          <el-input v-model="scope.row.sort" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" :min-width="parentGrade !== '2' ? '250' : '150'">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['bf:evaluation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row.id)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['bf:evaluation:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
          <el-button
            v-if="scope.row.grade !== '3'"
            v-hasPermi="['bf:evaluation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="addNextHandle(scope.row)"
          >{{ $t('新增下级指标') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基金评估模板对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <bfEvaluationDetailForm ref="bfEvaluationDetailForm" />
    </el-dialog>
  </div>
</template>

<script>
import { getAllData, delBfEvaluationDetail, updateSort } from '@/api/deal/bf/evaluation/bfEvaluationDetail'
import bfEvaluationDetailForm from './bfEvaluationDetailForm.vue'
export default {
    components: {
        bfEvaluationDetailForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 基金评估模板表格数据
          bfEvaluationDetailList: [],
          // 弹出层标题
          title: '基金评估模板',
          // 是否显示弹出层
          formOpen: false,
          // 级别字典
          gradeOptions: [],
          // 当前父级别
          parentGrade: '0',
          // 基金评估模板类型ID
          typeId: undefined,
          // 查询参数
          queryParams: {
              typeId: undefined,
              indicators: undefined,
              parentId: '0',
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('bf_evaluation_grade').then(response => {
          this.gradeOptions = response.data
      })
  },
  methods: {
    init(data) {
      // 基金评估模板类型ID
      this.typeId = data.id
      this.queryParams.typeId = data.id
      this.getList()
    },
    /** 查询基金评估模板列表 */
    getList() {
        this.loading = true
        getAllData(this.queryParams).then(response => {
            this.bfEvaluationDetailList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 级别字典翻译
    gradeFormat(row) {
      return this.selectDictLabel(this.gradeOptions, row.grade)
    },
    // 查看下级指标
    viewNext(row) {
      this.parentGrade = row.grade
      this.queryParams.parentId = row.id
      this.getList()
    },
    // 保存排序
    submitSort() {
      var data = { children: [] }
      // 保存传值时只传ID和排序
      for (let i = 0; i < this.bfEvaluationDetailList.length; i++) {
        data.children.push({ sort: this.bfEvaluationDetailList[i].sort, id: this.bfEvaluationDetailList[i].id, children: this.bfEvaluationDetailList[i].children })
      }
      this.loading = true
      updateSort(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.loading = false
          this.msgSuccess(this.$t('修改成功'))
        } else {
          this.msgError(response.msg)
        }
      })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            grade: undefined,
            indicators: undefined,
            weight: undefined,
            sort: undefined,
            parentId: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.queryParams.indicators = ''
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.bfEvaluationDetailForm.init(null, this.typeId)
        })
    },
    // 新增下级指标
    addNextHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.bfEvaluationDetailForm.initNext(row, this.typeId)
      })
    },
    /** 修改按钮操作 */
    updateHandle(id) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.bfEvaluationDetailForm.init(id, this.typeId)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delBfEvaluationDetail(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    }
  }
}
</script>
